import { createApp } from 'vue'
import App from './App.vue'
import router from './router/router'
import '../style.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import store from './store/store'
import paneHead from './components/paneHead.vue'

//刷新后的动态路由添加
const localData = localStorage.getItem('pc_pz')
if (localData) {
  store.commit('dynamicMenu', JSON.parse(localData).menu.routerList)
  store.state.menu.routerList.forEach(item => {
    router.addRoute('main',item)
  })
}
// 路由导航守卫
router.beforeEach((to, from) => {
  const token = localStorage.getItem('pz_token')
  // 判断非登录页面且token不存在
  if (!token&&to.path!=='/login') {
    return '/login'
  } else if (token && to.path == '/login') {
    return '/'
  } else {
    return true
  }
})
const app = createApp(App)
// 路由挂载
app.use(router)
// 挂载vuex的store
app.use(store)
    app.mount('#app')
    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
        app.component(key, component)
    }
      
//全局注册组件
app.component('paneHead',paneHead)